package com.bdcom.autotest;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.bdcom.autotest.util.CUtil;
import com.bdcom.autotest.util.Prop;
import com.bdcom.autotest.util.UIUtil;
import com.bdcom.autotest.util.VerticalFlowLayout;


public class LoginFrame extends JFrame implements ActionListener{

	private static Logger log = LoggerFactory.getLogger(LoginFrame.class);
	
	private static LoginFrame loginFrame;
	
	public static LoginFrame getInstance() {
		if( loginFrame == null ) {
			loginFrame = new LoginFrame();
		}
		return loginFrame;
	}

	
	public LoginFrame() {
		
		initUI();
		
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
				int result = JOptionPane.showConfirmDialog( loginFrame , ("确定要关闭 ?") );
				if( 0 == result ){
					System.exit(0);
				}
			}
		});
	}
	
	
	private JPanel mainp ;
	private JTextField unameField;
	private JPasswordField passField;
	private JTextField ipField;
	private JButton loginBtn;
	
	private void initUI() {
		this.setTitle("BDCOM AUTO TEST LOGIN");
		
		mainp = UIUtil.createPanel();
		mainp.setLayout(new VerticalFlowLayout() );
		
		unameField = new JTextField(15);
		unameField.setFont(UIUtil.font1);
		passField = new JPasswordField(15);
		passField.setFont(UIUtil.font1);
		ipField = new JTextField(15);
		ipField.setFont(UIUtil.font1);
		
		JLabel unamelabel = new JLabel("用户名");
		JLabel passlabel = new JLabel("密码");
		JLabel iplabel = new JLabel("服务器IP");
		
		JPanel p1 = UIUtil.createPanel();
		p1.setLayout(new FlowLayout( FlowLayout.LEFT ) );
		p1.add(Box.createHorizontalStrut(80));
		p1.add(unamelabel);
		p1.add(Box.createHorizontalStrut(30));
		p1.add(unameField);
		
		
		JPanel p2 = UIUtil.createPanel();
		p2.setLayout(new FlowLayout( FlowLayout.LEFT ) );
		p2.add(Box.createHorizontalStrut(80));
		p2.add(passlabel);
		p2.add(Box.createHorizontalStrut(43));
		p2.add(passField);
		
		JPanel p3 = UIUtil.createPanel();
		p3.setLayout(new FlowLayout( FlowLayout.LEFT ) );
		p3.add(Box.createHorizontalStrut(80));
		p3.add(iplabel);
		p3.add(Box.createHorizontalStrut(19));
		p3.add(ipField);
		
		unameField.setText("root");
		passField.setText("public");
		ipField.setText("127.0.0.1");
		
		
		JPanel p4 = UIUtil.createPanel();
		loginBtn = new JButton("  登陆  ");
		loginBtn.addActionListener(this);
		p4.add(loginBtn);
		
		JButton bbbb = new JButton("  TTTT  ");
		bbbb.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				keepAlive();
			}
		});
		
//		p4.add(bbbb);
		
		JPanel pv1 = UIUtil.createPanel();
		pv1.setPreferredSize(new Dimension( 0,  80 ) );
		mainp.add(pv1);
		
		mainp.add(p1);
		mainp.add(p2);
		mainp.add(p3);
		mainp.add(p4);
		
		this.getContentPane().add(mainp);
	}


	public void showFrame() {
		this.setSize( 500, 360 );
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
	}
	
	
	public void login() {
		
		loginBtn.setText("  登陆中...  ");
		loginBtn.setEnabled(false);
		
		new Thread() {
			public void run()  {
				doLogin();
			};
		}.start();
	}
	
	public void doLogin() {
		String text = ipField.getText().trim();
		Prop.serverIP = text;

		JSONObject args = new JSONObject();
		String user = unameField.getText().trim() ;
		args.put("logname", user);
		String pass = String.valueOf(passField.getPassword());
		args.put("logpass", pass );
		
		String reqUrl = "/login2";
		JSONObject httpPost = null;
		try {
			httpPost = CUtil.httpPost(args, reqUrl);
			String res = httpPost.getString(CUtil.RES);
			String sessionId = httpPost.getString(CUtil.SID);
			CUtil.SESSIONID = sessionId;
			
			log.debug(  "login result : " +  res  );
			
			if(  "true".equals(res) ) {
				Prop.username = user;
				//获取权限
				//...
				
				BootThread bootThread = new BootThread();
				bootThread.start();
				MainFrame.getInstance().showFrame( bootThread );
				this.dispose();
			} else {
				JOptionPane.showMessageDialog(this, "用户名或密码错误！");
			}
		} catch (Exception e) {
			log.error("" , e );
			JOptionPane.showMessageDialog(this, "服务器错误，请保证服务器地址填写正确，并且服务器正常运行！");
		}
		
		loginBtn.setText("  登陆  ");
		loginBtn.setEnabled(true);
		
		
	}

	
	public void keepAlive() {
		
		JSONObject args = new JSONObject();
		args.put("sid", CUtil.SESSIONID);
		
		String reqUrl = "/keepalive2";
		JSONObject httpPost = CUtil.httpPost(args, reqUrl);
		
		String res = httpPost.getString(CUtil.RES);
		
		System.out.println(  "keepalive result : " +  res  );
		
	}
	
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		login();
	}
	
	
	public static void main(String[] args) {
		Prop.init(args);
		getInstance().showFrame();
		
	}
	
	
	
}
